﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mimoza.AgentLayer
{
    /// <summary>
    /// Provide settings for process.
    /// </summary>
    public class Settings
    {
        public Settings()
        {
            Init();
            m_globalSettings = this;
        }

        public static Settings GlobalSettings
        {
            get { return m_globalSettings; }
        }

        public static void Save(Settings settings)
        {
            SettingsRegistry registry = new SettingsRegistry();

            registry.Save(settings);
        }

        public static void Load(Settings settings)
        {
            SettingsRegistry registry = new SettingsRegistry();

            registry.Load(settings);
        }

        public void Init()
        {
            m_broadcastPort = 7000;
            m_serverPort = 5000;
        }

        public int BroadcastTransportPort
        {
            get { return m_broadcastPort; }
            set { m_broadcastPort = value; }
        }

        public int HostUDPTransportPort
        {
            get { return (!IsOnServerHost) ? m_broadcastPort : m_broadcastPort+1; }
        } 
        
        public int ServerTransportPort
        {
            get { return m_serverPort; }
            set { m_serverPort = value; }
        }

        public int FindServerPeriod
        {
            get { return m_findServerPeriod; }
            set { m_findServerPeriod = value; }
        }

        public string ServerHost
        {
            get { return m_serverHostName; }
            set { m_serverHostName = value; }
        }

        public string ServerAddr
        {
            get { return m_serverHostAddr; }
            set { m_serverHostAddr = value; }
        }

        public int AcceptedFindServerRequestNumber
        {
            get { return m_acceptedFindServerRequestNumber; }
            set { m_acceptedFindServerRequestNumber = value; }
        }

        public string HostId
        {
            get { return m_hostId; }
            set { m_hostId = value; }
        }

        public Guid PolicySynchronizationId
        {
            get { return m_policySyncId; }
            set { m_policySyncId = value; }
        }

        public int CreateConnectionPeriod
        {
            get { return m_createConnectionPeriod; }
            set { if (value>0) m_createConnectionPeriod = value; }
        }

        public bool IsTimeSynchronizeFromServer
        {
            get { return m_isTimeSynchorize; }
            set { m_isTimeSynchorize = value; }
        }

        // this agent run on the same computer with mimoza service 
        public bool IsOnServerHost
        {
            get { return m_bIsOnServerHost; }
            set { m_bIsOnServerHost = value; }
        }

        static Settings m_globalSettings = null;

        int m_broadcastPort = 0;
        int m_serverPort = 0;
        bool m_bIsOnServerHost = false;
                
        int m_acceptedFindServerRequestNumber = 5;  // each 5 request from other host will be accepted by this host
        int m_findServerPeriod = 5000;              // 5 secs

        string m_hostId = "";

        string m_serverHostName = "";
        string m_serverHostAddr = "";

        Guid m_policySyncId;

        bool m_isTimeSynchorize = false;

        int m_createConnectionPeriod = Common.GlobalSettings.c_DefaultPingTimeout; // in secs
    }
}
